<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：海南芒点网络科技有限公司
 * 授权域名：kf.hnmangdian.com
 * 授权码：iuGYepyTlKxWomKwhgsupaLmF
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

// +----------------------------------------------------------------------
// | 商户 - 余额管理
// +----------------------------------------------------------------------
namespace app\controller;
use pay\wechatpay\WxPayV3;
use think\facade\View;
use think\facade\Db;

class BusinessMoney extends Common
{
	//余额明细
    public function moneylog(){
		if(request()->isAjax()){
			$page = input('param.page');
			$limit = input('param.limit');
			if(input('param.field') && input('param.order')){
				$order = 'business_moneylog.'.input('param.field').' '.input('param.order');
			}else{
				$order = 'business_moneylog.id desc';
			}
			$where = [];
			$where[] = ['business_moneylog.aid','=',aid];
			if(bid != 0){
				$where[] = ['business_moneylog.bid','=',bid];
			}else{
				if(input('param.bid')) $where[] = ['business_moneylog.bid','=',trim(input('param.bid'))];
                }
			if(input('param.name')) $where[] = ['business.name','like','%'.trim(input('param.name')).'%'];
			if(input('?param.status') && input('param.status')!=='') $where[] = ['business_moneylog.status','=',input('param.status')];
			$count = 0 + Db::name('business_moneylog')->alias('business_moneylog')->field('business.name,business_moneylog.*')->join('business business','business.id=business_moneylog.bid')->where($where)->count();
			$data = Db::name('business_moneylog')->alias('business_moneylog')->field('business.name,business_moneylog.*')->join('business business','business.id=business_moneylog.bid')->where($where)->page($page,$limit)->order($order)->select()->toArray();
			return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
		}
		return View::fetch();
    }
	//余额明细导出
	public function moneylogexcel(){
		if(input('param.field') && input('param.order')){
			$order = 'business_moneylog.'.input('param.field').' '.input('param.order');
		}else{
			$order = 'business_moneylog.id desc';
		}
		$where = [];
		$where[] = ['business_moneylog.aid','=',aid];
		if(bid != 0){
			$where[] = ['business_moneylog.bid','=',bid];
		}else{
			if(input('param.bid')) $where[] = ['business_moneylog.bid','=',trim(input('param.bid'))];
		}
		if(input('param.name')) $where[] = ['business.name','like','%'.trim(input('param.name')).'%'];
		if(input('?param.status') && input('param.status')!=='') $where[] = ['business_moneylog.status','=',input('param.status')];
		$list = Db::name('business_moneylog')->alias('business_moneylog')->field('business.name,business_moneylog.*')->join('business business','business.id=business_moneylog.bid')->where($where)->order($order)->select()->toArray();
		$title = array();
		$title[] = '商户名称';
		$title[] = '变更金额';
		$title[] = '变更后剩余';
		$title[] = '变更时间';
		$title[] = '备注';
		$data = array();
		foreach($list as $v){
			$tdata = array();
			$tdata[] = $v['name'];
			$tdata[] = $v['money'];
			$tdata[] = $v['after'];
			$tdata[] = date('Y-m-d H:i:s',$v['createtime']);
			$tdata[] = $v['remark'];
			$data[] = $tdata;
		}
		$this->export_excel($title,$data);
	}
	//余额明细改状态
	public function moneylogsetst(){
		if(bid > 0) showmsg('无操作权限');
		$ids = input('post.ids/a');
        $st = input('post.st/d');
		Db::name('business_moneylog')->where('aid',aid)->where('id','in',$ids)->update(['status'=>$st]);
		return json(['status'=>1,'msg'=>'删除成功']);
	}
	//余额明细删除
	public function moneylogdel(){
		if(bid > 0) showmsg('无操作权限');
		$ids = input('post.ids/a');
		Db::name('business_moneylog')->where('aid',aid)->where('id','in',$ids)->delete();
		\app\common\System::plog('删除商户余额明细'.implode(',',$ids));
		return json(['status'=>1,'msg'=>'删除成功']);
	}

    //余额提现
    public function withdraw(){
        $business = db('business')->where(array('id'=>bid))->find();
        $bset = db('business_sysset')->where(['aid'=>aid])->find();
        $admin_set = $this->adminSet;
        if(request()->isPost()){
            $info = input('post.info/a');
            $money = floatval($info['money']);
            if($money < $bset['withdrawmin']){
                return json(['status'=>0,'msg'=>'提现金额不能小于'.$bset['withdrawmin']]);
            }
            //if($money > 5000){
            //  return ['status'=>0,'msg'=>'单次提现金额不能大于5000'];
            //}
            if($business['money'] < $money) return json(['status'=>0,'msg'=>'可提现余额不足']);
            $data = array();
            $data['aid'] = aid;
            $data['bid'] = bid;
            $data['txmoney'] = $money;
            $data['money'] = $money * (1-$bset['withdrawfee']*0.01);
            $data['paytype'] = $info['paytype'];
            $data['ordernum'] = date('YmdHis').rand(1000,9999);
            $data['createtime'] = time();
            $data['status'] = 0;
            if($data['paytype'] == '银行卡'){
                $data['bankname'] = $business['bankname'];
                $data['bankcarduser'] = $business['bankcarduser'];
                $data['bankcardnum'] = $business['bankcardnum'];
                if($data['bankname']=='' || $data['bankcarduser']=='' || $data['bankcardnum']==''){
                    return json(['status'=>0,'msg'=>'请填写完整提现信息','url'=>(string)url('Backstage/sysset')]);
                }
                db('business')->where(['id'=>bid])->update(['bankname'=>$data['bankname'],'bankcarduser'=>$data['bankcarduser'],'bankcardnum'=>$data['bankcardnum']]);
            }else if($data['paytype'] == '微信' || $data['paytype'] == '微信钱包'){

                $is_weixin_withdraw_max = 0;
                //超额判断$is_withdraw_max 1：超额 0不超
                if($bset['commission_autotransfer'] ==1 && $money > $bset['weixin_withdraw_max'] && $bset['weixin_withdraw_max'] > 0){
                    $is_weixin_withdraw_max = 1;
                }
                $data['weixin'] = $business['weixin'];
                if($data['weixin']==''){
                    return json(['status'=>0,'msg'=>'请填写完整提现信息','url'=>(string)url('Backstage/sysset')]);
                }
                if($bset['commission_autotransfer']==1 && !$is_weixin_withdraw_max &&  $admin_set['wx_transfer_type']==0){
                    //是否超过限额
                    $mid = Db::name('admin_user')->where('aid',aid)->where('bid',bid)->where('isadmin',1)->value('mid');
                    if(!$mid) return json(['status'=>0,'msg'=>'商户主管理员未绑定微信']);
                }
                //db('agent')->where(['agid'=>$agid])->update(['weixin'=>$data['weixin']]);
            }else if($data['paytype'] == '支付宝'){
                $is_alipay_withdraw_max = 0;
                //超额判断$is_withdraw_max 1：超额 0不超
                if($bset['commission_autotransfer'] ==1 && $money > $bset['alipay_withdraw_max'] && $bset['alipay_withdraw_max'] > 0){
                    $is_alipay_withdraw_max = 1;
                }
                $data['aliaccount'] = $business['aliaccount'];
                if($data['aliaccount']==''){
                    return json(['status'=>0,'msg'=>'请填写完整提现信息','url'=>(string)url('Backstage/sysset')]);
                }
            }else{
                }

            $res = \app\common\Business::addmoney(aid,bid,-$money,'余额提现',false,'withdraw');
            if($res && $res['status'] == 0) return json(['status'=>0,'msg'=>$res['msg']]);

            $id = db('business_withdrawlog')->insertGetId($data);
            if(!$id) return json(['status'=>0,'msg'=>'提现失败']);

            \app\common\System::plog('商家提现'.$id);

            if($data['paytype'] == '微信' || $data['paytype'] == '微信钱包'){

                if($bset['commission_autotransfer']==1 && !$is_weixin_withdraw_max &&  $admin_set['wx_transfer_type']==0){

                    $rs = \app\common\Wxpay::transfers(aid,$mid,$data['money'],$data['ordernum'],'','余额提现');
                    if($rs['status']==0){
                        //\app\common\Business::addmoney(aid,bid,$money,'余额提现失败返还');
                        $data = [];
                        $data['status'] = 1;
                        $data['reason'] = $rs['msg']??'微信提现失败';
                        Db::name('business_withdrawlog')->where('id',$id)->update($data);
                        return json(['status'=>1,'msg'=>'提交成功,请等待打款','url'=>(string)url('withdrawlog')]);
                    }else{
                        $data = [];
                        $data['weixin'] = t('会员').'ID：'.$mid;
                        $data['status'] = 3;
                        $data['paytime']= time();
                        $data['paynum'] = $rs['resp']['payment_no'];
                        Db::name('business_withdrawlog')->where('id',$id)->update($data);

                        //提现成功通知
                        $tmplcontent = [];
                        $tmplcontent['first'] = '您的提现申请已打款，请留意查收';
                        $tmplcontent['remark'] = '请点击查看详情~';
                        $tmplcontent['money'] = (string) $data['money'];
                        $tmplcontent['timet'] = date('Y-m-d H:i',$data['createtime']);
                        $tempconNew = [];
                        $tempconNew['amount2'] = (string) round($data['money'],2);//提现金额
                        $tempconNew['time3'] = date('Y-m-d H:i',$data['createtime']);//提现时间
                        \app\common\Wechat::sendtmpl(aid,$mid,'tmpl_tixiansuccess',$tmplcontent,m_url('admin/index/index'),$tempconNew);
                        //短信通知
                        $member = Db::name('member')->where('id',$mid)->find();
                        if($member['tel']){
                            $tel = $member['tel'];
                            \app\common\Sms::send(aid,$tel,'tmpl_tixiansuccess',['money'=>$data['money']]);
                        }
                        
                        return json(['status'=>1,'msg'=>$rs['msg'],'url'=>(string)url('withdrawlog')]);
                    }
                }
            }else if($data['paytype'] == '支付宝'){
                if($bset['commission_autotransfer']==1 && !$is_alipay_withdraw_max){
                    $rs = \app\common\Alipay::transfers(aid,$data['ordernum'],$money,t('余额').'提现',$business['aliaccount'],$business['aliaccountname'],t('余额').'提现');
                    if($rs && $rs['status']==1){
                        $data = [];
                        $data['aliaccount'] = $business['aliaccount'];
                        $data['status'] = 3;
                        $data['paytime'] = time();
                        $data['paynum'] = $rs['resp']['payment_no'];
                        Db::name('business_withdrawlog')->where('id',$id)->update($data);
                        \app\common\System::plog('商家提现支付宝打款'.$id);
                        return json(['status'=>1,'msg'=>$rs['msg'],'url'=>(string)url('withdrawlog')]);
                    }else{
                        $data = [];
                        $data['status'] = 1;
                        $data['reason'] = $rs['sub_msg']??'支付宝提现失败';
                        Db::name('business_withdrawlog')->where('id',$id)->update($data);
                        return json(['status'=>1,'msg'=>'提交成功,请等待打款','url'=>(string)url('withdrawlog')]);
                    }
                }
            }else{
                }

            if($bset['commission_autotransfer'] ==1 && $admin_set['wx_transfer_type']==1 && !$is_weixin_withdraw_max){
                if($data['paytype'] == '微信' || $data['paytype'] == '微信钱包') {
                    //使用了新版的商家转账功能
                    $mid = Db::name('admin_user')->where('aid', aid)->where('bid', bid)->where('isadmin', 1)->value('mid');
                    if (!$data['platform']) {
                        $member = Db::name('member')->where('id', $mid)->field('id,realname,wxopenid,mpopenid')->find();
                        $openid = $member['mpopenid'];
                        if (!$openid) {
                            $platform = 'wx';
                        } else {
                            $platform = 'mp';
                        }
                    } else {
                        $platform = $data['platform'];
                    }

                    //使用了新版的商家转账功能
                    $paysdk = new WxPayV3(aid, $mid, $platform);
                    $rs = $paysdk->transfer($data['ordernum'], $data['money'], '', t('余额') . '提现', 'business_withdrawlog', $id);
                    if ($rs['status'] == 1) {
                        $data_u = [
                            'status' => '4',//状态改为处理中，用户确认收货后再改为已打款
                            'wx_package_info' => $rs['data']['package_info'],//用户确认页面的信息
                            'wx_state' => $rs['data']['state'],//转账状态
                            'wx_transfer_bill_no' => $rs['data']['transfer_bill_no'],//微信单号
                            'platform' => $platform
                        ];
                        Db::name('business_withdrawlog')->where('id', $id)->update($data_u);
                    } else {
                        $data_u = [
                            'status' => 1,
                            'wx_transfer_msg' => $rs['msg'],
                            'platform' => $platform
                        ];
                        Db::name('business_withdrawlog')->where('id', $id)->update($data_u);
                    }
                }
            }
            return json(['status'=>1,'msg'=>'提交成功','url'=>(string)url('withdrawlog')]);
        }
        View::assign('money',$business['money']);
        View::assign('business',$business);
        View::assign('bset',$bset);
        View::assign('admin_set',$admin_set);
        return View::fetch();
    }

	//提现记录
	public function withdrawlog(){
		if(request()->isAjax()){
			$page = input('param.page');
			$limit = input('param.limit');
			if(input('param.field') && input('param.order')){
				$order = 'business_withdrawlog.'.input('param.field').' '.input('param.order');
			}else{
				$order = 'business_withdrawlog.id desc';
			}
			$where = [];
			$where[] = ['business_withdrawlog.aid','=',aid];
			if(bid != 0){
				$where[] = ['business_withdrawlog.bid','=',bid];
			}else{
				if(input('param.bid')) $where[] = ['business_withdrawlog.bid','=',trim(input('param.bid'))];
			}
			if(input('id')){
                $where[] = ['business_withdrawlog.id','=',input('id')];
            }
			if(input('param.name')) $where[] = ['business.name','like','%'.trim(input('param.name')).'%'];
			if(input('?param.status') && input('param.status')!=='') $where[] = ['business_withdrawlog.status','=',input('param.status')];
			$count = 0 + Db::name('business_withdrawlog')->alias('business_withdrawlog')->field('business.mid,business.name,business_withdrawlog.*')->join('business business','business.id=business_withdrawlog.bid')->where($where)->count();
			$data = Db::name('business_withdrawlog')->alias('business_withdrawlog')->field('business.mid,business.name,business_withdrawlog.*')->join('business business','business.id=business_withdrawlog.bid')->where($where)->page($page,$limit)->order($order)->select()->toArray();
			foreach($data as $k=>$v){
				$mid = Db::name('admin_user')->where('aid',aid)->where('bid',$v['bid'])->where('isadmin',1)->value('mid');
				if($mid){
					$member = Db::name('member')->where('aid',aid)->where('id',$mid)->find();
					$data[$k]['headimg'] = $member['headimg'];
					$data[$k]['nickname'] = $member['nickname'];
				}else{
					$data[$k]['headimg'] = '';
					$data[$k]['nickname'] = '';
				}
                }
			return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
		}
		return View::fetch();
    }
	//提现记录导出
	public function withdrawlogexcel(){
		if(input('param.field') && input('param.order')){
			$order = 'business_withdrawlog.'.input('param.field').' '.input('param.order');
		}else{
			$order = 'business_withdrawlog.id desc';
		}
        $page = input('param.page')?:1;
        $limit = input('param.limit')?:10;
		$where = [];
		$where[] = ['business_withdrawlog.aid','=',aid];
		if(bid != 0){
			$where[] = ['business_withdrawlog.bid','=',bid];
		}else{
			if(input('param.bid')) $where[] = ['business_withdrawlog.bid','=',trim(input('param.bid'))];
		}
		if(input('param.name')) $where[] = ['business.name','like','%'.trim(input('param.name')).'%'];
		if(input('?param.status') && input('param.status')!=='') $where[] = ['business_withdrawlog.status','=',input('param.status')];
		$list = Db::name('business_withdrawlog')->alias('business_withdrawlog')->field('business.name,business_withdrawlog.*')
            ->join('business business','business.id=business_withdrawlog.bid')
            ->where($where)->order($order)->page($page,$limit)->select()->toArray();
        $count = Db::name('business_withdrawlog')->alias('business_withdrawlog')->field('business.name,business_withdrawlog.*')
            ->join('business business','business.id=business_withdrawlog.bid')
            ->where($where)->order($order)->count();
		$title = array();
		$title[] = '商户名称';
		$title[] = '提现金额';
		$title[] = '打款金额';
		$title[] = '提现方式';
		$title[] = '收款账号';
		$title[] = '提现时间';
		$title[] = '状态';
		$data = array();
		foreach($list as $v){
			$tdata = array();
			$tdata[] = $v['name'];
			$tdata[] = $v['txmoney'];
			$tdata[] = $v['money'];
			$tdata[] = $v['paytype'];
			if($v['paytype'] == '支付宝'){
				$tdata[] = $v['aliaccount'];
			}elseif($v['paytype'] == '银行卡'){
				$tdata[] = $v['bankname'] . ' - ' .$v['bankcarduser']. ' - '.$v['bankcardnum'];
			}elseif($v['paytype'] == '微信'){
				$tdata[] = $v['weixin'];
			}else{
				$tdata[] = '';
			}
			$tdata[] = date('Y-m-d H:i:s',$v['createtime']);
			$st = '';
			if($v['status']==0){
				$st = '审核中';
			}elseif($v['status']==1){
				$st = '已审核';
			}elseif($v['status']==2){
				$st = '已驳回';
			}elseif($v['status']==3){
				$st = '已打款';
			}
			$tdata[] = $st;
			$data[] = $tdata;
		}
        return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data,'title'=>$title]);
		$this->export_excel($title,$data);
	}
	//提现记录改状态
	public function withdrawlogsetst(){
		if(bid > 0) showmsg('无操作权限');
		$id = input('post.id/d');
		$st = input('post.st/d');
		$reason = input('post.reason');
		$info = Db::name('business_withdrawlog')->where('aid',aid)->where('id',$id)->find();
        $info['money'] = dd_money_format($info['money']);
        $info['txmoney'] = dd_money_format($info['txmoney']);
		$mid = Db::name('admin_user')->where('aid',aid)->where('bid',$info['bid'])->where('isadmin',1)->value('mid');
		if($st==10){//微信打款
			if($info['status']!=1) return json(['status'=>0,'msg'=>'已审核状态才能打款']);
			if(!$mid) return json(['status'=>0,'msg'=>'商户未绑定微信']);
			if(!$info['platform']){
                $member = Db::name('member')->where('id',$mid)->field('id,realname,wxopenid,mpopenid')->find();
                $openid = $member['mpopenid'];
                if(!$openid){
                    $platform = 'wx';
                }else{
                    $platform = 'mp';
                }
            }else{
                $platform = $info['platform'];
            }
            $admin_set = $this->adminSet;
            if($admin_set['wx_transfer_type']==1){
                //使用了新版的商家转账功能
                $paysdk = new WxPayV3(aid,$mid,$platform);
                $rs = $paysdk->transfer($info['ordernum'],$info['money'],'',t('余额').'提现','business_withdrawlog',$info['id']);
                if($rs['status']==1){
                    $data = [
                        'status' => '4',//状态改为处理中，用户确认收货后再改为已打款
                        'wx_package_info' => $rs['data']['package_info'],//用户确认页面的信息
                        'wx_state' => $rs['data']['state'],//转账状态
                        'wx_transfer_bill_no' => $rs['data']['transfer_bill_no'],//微信单号
                        'platform' => $platform
                    ];
                    Db::name('business_withdrawlog')->where('id',$info['id'])->update($data);
                }else{
                    $data = [
                        'wx_transfer_msg' => $rs['msg'],
                        'platform' => $platform
                    ];
                    Db::name('business_withdrawlog')->where('id',$info['id'])->update($data);
                }
            }else {
                $rs = \app\common\Wxpay::transfers(aid, $mid, $info['money'], $info['ordernum'], '', '余额提现');
                if($rs['status']==1){
                    Db::name('business_withdrawlog')->where('aid',aid)->where('id',$id)->update(['status'=>3,'reason'=>$reason,'paytime'=>time(),'paynum'=>$rs['resp']['payment_no']]);
                }
            }
			if($rs['status']==0){
				return json(['status'=>0,'msg'=>$rs['msg']]);
			}else{
				//提现成功通知
				$tmplcontent = [];
				$tmplcontent['first'] = '您的提现申请已打款，请留意查收';
				$tmplcontent['remark'] = '请点击查看详情~';
				$tmplcontent['money'] = (string) $info['money'];
				$tmplcontent['timet'] = date('Y-m-d H:i',$info['createtime']);
                $tempconNew = [];
                $tempconNew['amount2'] = (string) round($info['money'],2);//提现金额
                $tempconNew['time3'] = date('Y-m-d H:i',$info['createtime']);//提现时间
				\app\common\Wechat::sendtmpl(aid,$mid,'tmpl_tixiansuccess',$tmplcontent,m_url('admin/index/index'),$tempconNew);
				//短信通知
				$member = Db::name('member')->where('id',$mid)->find();
				if($member['tel']){
					$tel = $member['tel'];
					\app\common\Sms::send(aid,$tel,'tmpl_tixiansuccess',['money'=>$info['money']]);
				}
				\app\common\System::plog('商家提现微信打款'.$id);
				return json(['status'=>1,'msg'=>$rs['msg']]);
			}
		}else if($st == 20){
            }else if($st==30){
            }else if($st==40){
            }else{
			Db::name('business_withdrawlog')->where('aid',aid)->where('id',$id)->update(['status'=>$st,'reason'=>$reason]);
			if($st == 2){//驳回返还余额
				\app\common\Business::addmoney(aid,$info['bid'],$info['txmoney'],'余额提现返还');
				//提现失败通知
				$tmplcontent = [];
				$tmplcontent['first'] = '您的提现申请被商家驳回，可与商家协商沟通。';
				$tmplcontent['remark'] = $reason.'，请点击查看详情~';
				$tmplcontent['money'] = (string) $info['txmoney'];
				$tmplcontent['time'] = date('Y-m-d H:i',$info['createtime']);
				\app\common\Wechat::sendtmpl(aid,$mid,'tmpl_tixianerror',$tmplcontent,m_url('admin/index/index'));
				//短信通知
				$member = Db::name('member')->where('id',$mid)->find();
				if($member['tel']){
					$tel = $member['tel'];
					$rs = \app\common\Sms::send(aid,$tel,'tmpl_tixianerror',['reason'=>$reason]);
				}
				\app\common\System::plog('商家提现驳回'.$id);
			}
			if($st==3){
				//提现成功通知
				$tmplcontent = [];
				$tmplcontent['first'] = '您的提现申请已打款，请留意查收';
				$tmplcontent['remark'] = '请点击查看详情~';
				$tmplcontent['money'] = (string) $info['money'];
				$tmplcontent['timet'] = date('Y-m-d H:i',$info['createtime']);
                $tempconNew = [];
                $tempconNew['amount2'] = (string) round($info['money'],2);//提现金额
                $tempconNew['time3'] = date('Y-m-d H:i',$info['createtime']);//提现时间
				\app\common\Wechat::sendtmpl(aid,$mid,'tmpl_tixiansuccess',$tmplcontent,m_url('admin/index/index'),$tempconNew);
				//短信通知
				$member = Db::name('member')->where('id',$mid)->find();
				if($member['tel']){
					$tel = $member['tel'];
					$rs = \app\common\Sms::send(aid,$tel,'tmpl_tixiansuccess',['money'=>$info['money']]);
				}
				\app\common\System::plog('商家提现改为已打款'.$id);
			}
		}
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	//提现记录删除
	public function withdrawlogdel(){
		if(bid > 0) showmsg('无操作权限');
		$ids = input('post.ids/a');
		Db::name('business_withdrawlog')->where('aid',aid)->where('id','in',$ids)->delete();
		\app\common\System::plog('删除商家提现记录'.implode(',',$ids));
		return json(['status'=>1,'msg'=>'删除成功']);
	}

    //明细
    public function depositlog(){
        }

    //明细删除
    public function depositlogdel(){
        if(bid > 0) showmsg('无操作权限');
        $ids = input('post.ids/a');
        Db::name('business_depositlog')->where('aid',aid)->where('id','in',$ids)->delete();
        \app\common\System::plog('删除商户保证金明细'.implode(',',$ids));
        return json(['status'=>1,'msg'=>'删除成功']);
    }
    //明细导出
    public function depositlogexcel(){
        }

    //余额转账
    public function transfer(){
        $business = db('business')->where(array('id'=>bid))->find();
        $bset = db('business_sysset')->where(['aid'=>aid])->find();

        if(request()->isPost()){
            $info = input('post.info/a');
            $money = floatval($info['money']);
            $tomid = trim($info['tomid']);
            $type = $info['type'];

            $tomember = Db::name('member')->where('aid',aid)->where('id',$tomid)->find();
            if(!$tomember){
                return json(['status'=>0,'msg'=>'转入会员不存在']);
            }
            
            if($business['money'] < $money) return json(['status'=>0,'msg'=>'可转账余额不足']);
            // 实际到账 去掉手续费
            $tomoney = dd_money_format($money * (1-$bset['withdrawfee']*0.01));

            $res = \app\common\Business::addmoney(aid,bid,-$money,'余额转账');
            if ($res['status'] == 1 && $tomoney>0) {
                if($type == 'money'){
                    \app\common\Member::addmoney(aid,$tomid,$tomoney,'商家'.$business['name'].'转账');
                }
                if($type == 'commission'){
                    \app\common\Member::addcommission(aid,$tomid,0,$tomoney,'商家'.$business['name'].'转账');
                }
            }else{
                return $this->json(['status'=>0, 'msg' => '转账失败']);
            }
            \app\common\System::plog('商家余额转账'.$id);
            return json(['status'=>1,'msg'=>'转账成功','url'=>(string)url('moneylog')]);
        }
        View::assign('money',$business['money']);
        return View::fetch();
    }

}
